package com.uh.calculation.batchOperator;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.uh.calculation.bean.*;
import com.uh.calculation.util.FormulaBatchUtil;
import com.uh.calculation.util.FormulaUtil;
import com.uh.calculation.util.GetBean;
import com.uh.calculation.util.TimeUtil;
import com.uh.microservice.core.dao.JdbcDao;
import lombok.Data;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 年报--月报字段匹配  逻辑为月报数据/年报数据
 */
@Data
public class NbBathOperator {
    private JdbcDao jdbcDao;
    private WarnJiSuanBean warnJiSuanBean;


    private SqlWriteBean sqlWriteBean;




    public NbBathOperator(JdbcDao jdbcDao, WarnJiSuanBean warnJiSuanBean, SqlWriteBean sqlWriteBean){
        this.jdbcDao=jdbcDao;
        this.warnJiSuanBean=warnJiSuanBean;
        this.sqlWriteBean=  sqlWriteBean;
    }
    /**
     * 季报数据
     * 月报数据
     * @param jb
     * @param list
     * @return
     */
    public   void jisuan(JSONArray jb , JSONArray yb , List<OperatorVal> list ){
        try {
            if (jb == null || yb == null || jb.size() == 0 || yb.size() == 0) {
                return;
            }
            this.warnJiSuanBean = warnJiSuanBean;
            Date lastTime = warnJiSuanBean.getLastTime();
            //反转2021 与 2022字段区别
            //GetFileds getFileds = new GetFileds(jdbcDao,list,warnJiSuanBean);
            for (OperatorVal operatorVal : list) {
                List<FormulaBean> formulaBeans = operatorVal.getFormulaBeans();//计算公式
                String field = operatorVal.getField();
                List<TbFeild> tbFeilds = GetBean.getTbf(field);
                String f1 = tbFeilds.get(0).getFieldName();
                String f2 = tbFeilds.get(1).getFieldName();
                //WarnJiSuanBean warnJiSuanBean = new WarnJiSuanBean();
                for (int i = 0; i < jb.size(); i++) {
                    try {
                        //for (Map m : jb) {
                        JSONObject object = jb.getJSONObject(i);
                        String timeCode = object.get("time_code").toString();
                        String snnum;
                        //高一周期的数据
                        String d2 = object.get(f1).toString();
                        Integer d1 = 0;
                        //从季报去比较月报
                        for (int j = 0; j < yb.size(); j++) {
                            JSONObject my = jb.getJSONObject(i);
                            //for (Map my : yb) {
                            String time = my.get("time_code").toString();
                            if (timeCode.substring(0, 4).equals(time.substring(0, 4))) {
                                try {
                                    //时间游标判断，及核查规则是否符合
                                    if (TimeUtil.bj(lastTime, timeCode, sqlWriteBean.getYear_is_month(), sqlWriteBean.getIs_hc())) {
                                        //取月报数据
                                        //String d1 = my.get(f1).toString();
                                        //取季报数据
                                        d1 += Integer.parseInt(my.get(f2).toString());
                                    }
                                } catch (Exception e) {
                                    //e.printStackTrace();
                                }
                            }
                        }
                        //根据当前计算类解析规则  将数据和字段对应
                        tbFeilds.get(0).setData(d1.toString());
                        tbFeilds.get(1).setData(d2);
                        //扔进公式解析工具
                        sqlWriteBean.setDimension(operatorVal.getDimension());
                        sqlWriteBean.setTimeCode(timeCode);
                        new FormulaBatchUtil(tbFeilds, formulaBeans, warnJiSuanBean, sqlWriteBean);
                    }catch (Exception e){

                    }


                }
            }
        }catch (Exception e){
            //e.printStackTrace();
        }

    }
}
